#include <QIcon>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPalette>
#include <QLabel>
#include <cmath>
#include <QTimer>
#include <QPushButton>
#include <QPixmap>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow), a(300,15),sub(this)

{
    a.mapgen(300);
    a.civdis(300,15);
    sub.setParent(this);
    for(int i=0;i<300;i+=1)
    {
        a.s[i].debugwindow=&sub;
        a.s[i].setText("");
        a.s[i].setParent(this);
        a.s[i].move(a.s[i].x,a.s[i].y);
        a.s[i].setMaximumSize(20,20);
        a.s[i].setMinimumSize(20,20);
        a.s[i].setIcon( a.s[i].button_icon);//加上图标
        a.s[i].setFlat(true);
        a.s[i].show();
        connect(&a.s[i],&QPushButton::pressed,&a.s[i],&star::select);
    }
    setFixedSize(1200,700);
    //sub.show();

    sub.pal.setBrush(QPalette::Window,QBrush(QPixmap(":/new/images/images/PureBlack.jpg")));
    sub.setPalette(sub.pal);

    QTimer *timer = new QTimer(this);   //初始化一个定时器。
    connect(timer, &QTimer::timeout, this, &MainWindow::update);   //设置信号与槽 ，槽函数update()需要再类中声明一下。
    timer->start(10 / 1);    //设置定时器刷新频率，即变量刷新频率。

    QTimer *turn = new QTimer(this);   //初始化一个定时器。
    connect(turn, &QTimer::timeout, this, &MainWindow::turn);   //设置信号与槽 ，槽函数update()需要再类中声明一下。
    turn->start(100 / 1);
}



MainWindow::~MainWindow()
{
    sub.close();
    delete ui;
}

void MainWindow::update()
{
    char buffer[256];
    sprintf(buffer, "回合:%d",t);
    sub.a.setText(buffer);
    sub.test.setText(buffer);
    sub.a.adjustSize();
}

void MainWindow::turn()
{
    t+=1;
    a.observe();
    for(int i=0;i<a.n_civ;i+=1)
    {
        a.c[i].nextTurn();
    }
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPixmap image2;
    image2.load(":/new/images/images/background.jpg");

    QPainter painter(this);
    painter.drawPixmap(0,0,900,600,image2);

    QPixmap image1;
    image1.load(":/new/images/images/icon_1.png");
    QPainter painter2(this);
    painter2.drawPixmap(900,0,1200,600,image1);
}



